package holder;

import java.io.File;
import java.nio.MappedByteBuffer;

import manager.DownLoadInfo;
import manager.DownLoadManager;
import manager.DownLoadManager.DownloadInfoObserver;

import com.example.googlemarketdemo.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import factory.ThreadPoolExecutorProxyFactory;

import utils.CommonUtils;
import utils.FileUtils;
import utils.PrintDownLoadInfo;
import utils.UIUtils;
import view.ProgressButton;
import android.app.DownloadManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import base.BaseHolder;
import bean.AppInfoBean;

/**
 * 详情页面的按钮holder
 * 
 * @author Anson-pc
 * 
 */
public class AppDetailButtomHolder extends BaseHolder<AppInfoBean> implements
		OnClickListener,DownloadInfoObserver {

	private TextView mTv;

	/*-----------  获取按钮的控件 ----------*/
	@ViewInject(R.id.app_detail_download_btn_download)
	ProgressButton mProgressBtn;

	@ViewInject(R.id.app_detail_download_btn_favo)
	Button mBtnfavo;

	@ViewInject(R.id.app_detail_download_btn_share)
	Button mBtnShare;

	private AppInfoBean mAppInfoBean;

	/*--------- end ----------*/

	@Override
	protected View initHolderView() {
		View view = View.inflate(UIUtils.getContext(),
				R.layout.item_detail_button, null);
		ViewUtils.inject(this, view);
		// 设置点击事件
		mProgressBtn.setOnClickListener(this);
		return view;
	}

	@Override
	protected void refreshHolderView(AppInfoBean data) {
		// 绑定数据

		mAppInfoBean = data;

		/*-----------  根据不同的状态给用户提示 ----------*/
		// 状态在哪里？--》DownloadInfo 里面
		DownLoadInfo downLoadInfo = DownLoadManager.getInstance()
				.getDownLoadInfo(mAppInfoBean);
		/*
		 * public static final int STATE_UNDOWNLOAD = 0; public static final int
		 * STATE_DOWNLOADING = 1; public static final int STATE_PAUSEDOWNLOAD =
		 * 2; public static final int STATE_WAITINGDOWNLOAD = 3; public static
		 * final int STATE_DOWNLOADFAILED = 4; public static final int
		 * STATE_DOWNLOADED = 5; public static final int STATE_INSTALLED = 6;
		 */
		refreshProgressBtn(downLoadInfo);
	}

	/**
	 *  刷新按钮的UI
	 * @param downLoadInfo
	 */
	public void refreshProgressBtn(DownLoadInfo downLoadInfo) {
		//设置下载按钮的初始背景
		mProgressBtn.setBackgroundResource(R.drawable.selector_app_detail_bottom_normal);
		
		switch (downLoadInfo.curState) {
		case DownLoadManager.STATE_UNDOWNLOAD:// 未下载
			mProgressBtn.setText("下载");
			break;
		case DownLoadManager.STATE_DOWNLOADING:// 下载中
			mProgressBtn.setBackgroundResource(R.drawable.selector_app_detail_bottom_downloading);
			mProgressBtn.setmMax(downLoadInfo.max);
			mProgressBtn.setmProgress(downLoadInfo.progress);
			int progress = (int) (downLoadInfo.progress *100.0f/downLoadInfo.max+.5f);
			mProgressBtn.setText(progress+"%");
			break;
		case DownLoadManager.STATE_PAUSEDOWNLOAD:// 暂停下载
			mProgressBtn.setText("继续下载");
			break;
		case DownLoadManager.STATE_WAITINGDOWNLOAD:// 等待下载
			mProgressBtn.setText("等待中。。。");
			break;
		case DownLoadManager.STATE_DOWNLOADFAILED:// 下载失败
			mProgressBtn.setText("重试");
			break;
		case DownLoadManager.STATE_DOWNLOADED:// 下载完成
			mProgressBtn.setProgressEnable(false);
			mProgressBtn.setText("安装");
			break;
		case DownLoadManager.STATE_INSTALLED:// 安装完成
			mProgressBtn.setText("打开");
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		/*-----------  根据不同的状态触发不同的操作 ----------*/
		DownLoadInfo downLoadInfo = DownLoadManager.getInstance()
				.getDownLoadInfo(mAppInfoBean);
		switch (downLoadInfo.curState) {
		case DownLoadManager.STATE_UNDOWNLOAD:// 未下载
			// 执行下载
			doDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_DOWNLOADING:// 下载中
			// 暂停下载
			pauseDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_PAUSEDOWNLOAD:// 暂停下载
			doDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_WAITINGDOWNLOAD:// 等待下载
			cancelDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_DOWNLOADFAILED:// 下载失败
			doDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_DOWNLOADED:// 下载完成
			installApk(downLoadInfo);
			break;
		case DownLoadManager.STATE_INSTALLED:// 安装完成
			openApk(downLoadInfo);
			break;

		default:
			break;
		}
	}

	// DownLoadInfo downloadInfo = new DownLoadInfo();
	// downloadInfo.downloadurl = mAppInfoBean.downloadUrl;
	// // apk存到什么目录，apk的命名规则
	// String dir = FileUtils.getDir("apk");
	// // apk命名规则
	// String fileName = mAppInfoBean.packageName + ".apk";
	// File saveFile = new File(dir, fileName);
	// // 保存路径
	// downloadInfo.savePath = saveFile.getAbsolutePath();
	// downloadInfo.packageName = mAppInfoBean.packageName;
	// // 异步下载
	// // 得到下载管理器
	// DownLoadManager.getInstance().download(downloadInfo);

	/**
	 * 打开apk
	 * @param downLoadInfo
	 */
	private void openApk(DownLoadInfo downLoadInfo) {
		CommonUtils.openApp(UIUtils.getContext(), downLoadInfo.packageName);

	}

	/**
	 * 安装apk
	 * @param downLoadInfo
	 */
	private void installApk(DownLoadInfo downLoadInfo) {

		File apkFile = new File(downLoadInfo.savePath);
		CommonUtils.installApp(UIUtils.getContext(), apkFile);
	}

	/**
	 * 取消下载
	 * @param downLoadInfo
	 */
	private void cancelDownLoad(DownLoadInfo downLoadInfo) {
		// TODO Auto-generated method stub

	}

	/**
	 * 暂停下载
	 * @param downLoadInfo
	 */
	private void pauseDownLoad(DownLoadInfo downLoadInfo) {

		DownLoadManager.getInstance().pauseDownLoad(downLoadInfo);
	}

	/**
	 * 开始下载，断点下载，取消操作
	 * @param downLoadInfo
	 */
	private void doDownLoad(DownLoadInfo downLoadInfo) {
		 // 得到下载管理器
		 DownLoadManager.getInstance().download(downLoadInfo);

		
	}

	/*-----------  收到DownLoadInfo被修改的通知 ----------*/
	@Override
	public void onDownLoadInfoChange(final DownLoadInfo downLoadInfo) {
		
		//过滤，只有能显示对应的Holder才能显示下载进度，根据包名
		if (!downLoadInfo.packageName.equals(mAppInfoBean.packageName)) {
			return ;//退出当前onDownLoadInfoChange的方法
		}
		
		//打印状态
		PrintDownLoadInfo.printDownLoadInfo(downLoadInfo);
		UIUtils.postTaskSafely(new Runnable() {
			
			@Override
			public void run() {
				
				//更新按钮的ui
				refreshProgressBtn(downLoadInfo);
			}
		});
		
	}
	
	/**
	 * 1.添加观察者
	 * 2.手动发布一下
	 */
	public void addObserverAndNotify() {
		//1.添加观察者
		DownLoadManager.getInstance().addObserver(this);
		//2.手动发布信息
		DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(mAppInfoBean);
		DownLoadManager.getInstance().notifyObserver(downLoadInfo);
		
	}

}
